#!/bin/bash

ARCH=
STARTDIR=`pwd`
PACKAGEMAKER="/Developer/usr/bin/packagemaker"
WHOAMI=`whoami`
CURRENTUSER=`who | awk '{print $1}' | tail -n1`
TMPDIR=/tmp/work_create_packages-$$
VERSION=SVN-r32882-4.2.1


shellout(){
 echo 
 echo "ERROR in $1 commande";
 echo 
 exit 2;
}


if [ ! "$WHOAMI" = "root" ]
then
    echo "You are running this program with user $WHOAMI"
    echo "You must be root to create the package"
    echo "Try with sudo $0"
    echo
    exit 1
fi


LSCMD=`whereis ls | awk '{print $1}'`
ARCH=`file "$LSCMD"`

if echo "$ARCH" | grep -q "x86_64"
then
	ARCH="x86_64"
else
	ARCH="i386"
fi

if [ ! "$1" = "DO_NOT_COMPILE" ]
then
    echo
    echo "Video tools compilation : mencoder, mplayer, ... "
    echo "Take a coffee ..."
    echo
    ./compile_mplayer.sh 
else
    echo 
    echo "Skipping ./compile_mplayer.sh $2"
    echo
fi

echo "Creating tmp working directory : $TMPDIR"
mkdir $TMPDIR
chmod 755 templates_mplayer/Resources/*

cp -r -p templates_mplayer/Resources $TMPDIR/
cp -r -p templates_mplayer/Info.plist $TMPDIR/
cp -r -p templates_mplayer/Description.plist $TMPDIR/
sed -e "s/ARCH/$ARCH/g"         templates_mplayer/Description.plist > $TMPDIR/Description.plist
sed -e "s/VERSION/$VERSION/g"   templates_mplayer/Description.plist > $TMPDIR/Description.plist
sed -e "s/ARCH/$ARCH/g"         templates_mplayer/Info.plist > $TMPDIR/Info.plist
sed -e "s/VERSION/$VERSION/g"   templates_mplayer/Info.plist > $TMPDIR/Info.plist
sed -e "s/ARCH/$ARCH/g"         templates_mplayer/Resources/English.lproj/Description.plist > $TMPDIR/Resources/English.lproj/Description.plist
sed -e "s/VERSION/$VERSION/g"   templates_mplayer/Resources/English.lproj/Description.plist > $TMPDIR/Resources/English.lproj/Description.plist


mkdir -p ../../build

DMGFILE=$STARTDIR/../../build/mplayer-$VERSION-${ARCH}.dmg
PKGFILE=$STARTDIR/../../build/mplayer-$VERSION-${ARCH}.pkg

UNINSTALL_SCRIPT=/new_root_video_tools/usr/local/bin/uninstall-mplayer-$VERSION-${ARCH}.sh
echo "Creating uninstall script ..."
>$UNINSTALL_SCRIPT
chmod +x $UNINSTALL_SCRIPT
find /new_root_video_tools -type f | sed -e 's#/new_root_video_tools##g' | while read l_file
do
	echo "echo \"rm -f $l_file\"" >> $UNINSTALL_SCRIPT
	echo "rm -f \"$l_file\"" >> $UNINSTALL_SCRIPT
	l_dir=`dirname "$l_file"`
	echo "echo \"rmdir $l_dir\"" >> $UNINSTALL_SCRIPT
	echo "rmdir \"$l_dir\" 2> /dev/null" >> $UNINSTALL_SCRIPT
        echo "rm -f /var/db/receipts/org.idoit.mplayer.plist"  >> $UNINSTALL_SCRIPT
        echo "rm -f /var/db/receipts/org.idoit.mplayer.bom"  >> $UNINSTALL_SCRIPT
done


echo 
echo "Creating package $PACKAGEMAKER -build -p $PKGFILE ... (using /new_root_video_tools)"
rm -rf "$DMGFILE" "$PKGFILE"
$PACKAGEMAKER -build -p $PKGFILE -f /new_root_video_tools/ \
              -r $TMPDIR/Resources -ds -i $TMPDIR/Info.plist \
              -d $TMPDIR/Description.plist > /dev/null 2>&1 || shellout "$PACKAGEMAKER -build -p $PKGFILE"

echo "Creating packages $DMGFILE ..."
hdiutil create "$DMGFILE" -srcfolder "$PKGFILE" -ov \
            > /dev/null 2>&1  || shellout "ERROR hdiutil create $DMGFILE"

echo
echo
chown -R $CURRENTUSER:admin $DMGFILE
chown -R $CURRENTUSER:admin $PKGFILE
passwd=`cat ../../.hg/hgrc | grep "password" | sed -e "s/password=//g"`
du -sh $DMGFILE
du -sh $PKGFILE
echo

#echo "../../src/googlecode_upload.py -p kakola -u elfoiros -w ??????? -s \"mplayer-$VERSION-${ARCH}\" $DMGFILE"

#../../src/googlecode_upload.py -p kakola -u elfoiros -w $passwd -s "mplayer-$VERSION-${ARCH}" $DMGFILE

